package com.dfrz.bookstore.dao.impl;

import com.dfrz.bookstore.util.JdbcUtils;
import com.dfrz.bookstore.dao.Dao;
import com.dfrz.bookstore.entry.Order;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 类功能描述:
 *
 * @Author keyan
 * @Date 2021/5/20 18:58
 */
public class OrderDaoImpl implements Dao<Order> {

    @Override
    public boolean insert(Order order)  {
        String sql = "insert into `order` (`order_item_id`,`user_id`,`status`) values (?,?,?)";
        PreparedStatement prep = null;
        Connection conn = null;
        try {
            conn = JdbcUtils.getConnection();
            prep = conn.prepareStatement(sql);
            prep.setString(1,order.getOrder_item_id());
            prep.setInt(2,order.getUser_id());
            prep.setInt(3,order.getStatus());

            int i = prep.executeUpdate();
            if (i>0){
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                JdbcUtils.close(conn,prep,null);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public boolean deleteObjById(int i) {
        return false;
    }

    @Override
    public boolean updateByObj(Order order) {
        if (order.getId()==0){
            return false;
        }
        StringBuilder sb = new StringBuilder("update `order` set `id` = "+order.getId());
//        if (order.getOrder_item_id()!=0){
//            sb.append(" , `order_item_id` = '"+order.getOrder_item_id()+"' ");
//        }
//        if (order.getUser_id()!=0){
//            sb.append(" , `user_id` = '"+order.getUser_id()+"'");
//        }
        if (order.getStatus()!=0){
            sb.append(" , `status` = "+order.getStatus());
        }
        sb.append(" where `id` = "+order.getId());
        Connection conn = null;
        Statement stat = null;
        try {
            conn = JdbcUtils.getConnection();
            stat =  conn.createStatement();
            int i = stat.executeUpdate(sb.toString());
            if (i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                JdbcUtils.close(conn,stat,null);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public List<Order> findObjByObj(Order order) {
        List<Order> orders = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select * from   `order` where 1=1 ");
        if (order.getId()!= 0){
            sb.append(" and `id` = "+order.getId());
        }
        if (order.getOrder_item_id()!=null){
            sb.append(" and `order_item_id` = '"+order.getOrder_item_id()+"' ");
        }
        if (order.getUser_id()!=0){
            sb.append(" and `user_id` = '"+order.getUser_id()+"'");
        }
        if (order.getStatus()!=0){
            sb.append(" and `status` = "+order.getStatus());
        }
        Connection conn = null;
        Statement stat = null;
        ResultSet resultSet =null;
        try {
            conn= JdbcUtils.getConnection();
            stat = conn.createStatement();
            resultSet = stat.executeQuery(sb.toString());

            while (resultSet.next()){
                Order order1 = new Order();
                order1.setId(resultSet.getInt("id"));
                order1.setOrder_item_id(resultSet.getString("order_item_id"));
                order1.setUser_id(resultSet.getInt("user_id"));
                order1.setStatus(resultSet.getInt("status"));
                orders.add(order1);
            }
            /*返回查找到的订单*/
            return orders;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                JdbcUtils.close(conn,stat,resultSet);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    public List<Order> findObjByPage(Order order, int pageNum, int pageSize) {
        int start = (pageNum-1)*pageSize;
        List<Order> orders = new ArrayList<>();
        StringBuilder sql = new StringBuilder("select * from `order` where 1 = 1 ");

        if (order.getId()!=0){
            sql.append(" and `id` = "+order.getId());
        }
        if (order.getOrder_item_id()!=null){
            sql.append(" and `order_item_id` = '"+order.getOrder_item_id()+"' ");
        }
        if (order.getUser_id()!=0){
            sql.append(" and `user_id` = "+order.getUser_id());
        }
        if (order.getStatus()!=0){
            sql.append(" and `status` = "+order.getStatus());
        }
        sql.append(" limit "+ start +","+pageSize);
        Connection conn = null;
        Statement stat = null;
        ResultSet resultSet =null;
        try {
            conn= JdbcUtils.getConnection();
            stat = conn.createStatement();
            resultSet = stat.executeQuery(sql.toString());

            while (resultSet.next()){
                Order order1 = new Order();
                order1.setId(resultSet.getInt("id"));
                order1.setOrder_item_id(resultSet.getString("order_item_id"));
                order1.setUser_id(resultSet.getInt("user_id"));
                order1.setStatus(resultSet.getInt("status"));
                orders.add(order1);
            }
            /*返回分页查找到的订单*/
            return orders;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                JdbcUtils.close(conn,stat,resultSet);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
